package org.example.security.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.example.common.utils.DateTimeUtil;
import org.example.db.entity.Role;
import org.example.db.mapper.RoleMapper;
import org.example.db.mapper.UserRoleMapper;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;


import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class RoleService {

    RoleMapper roleMapper;
    UserRoleMapper userRoleMapper;

    public RoleService(RoleMapper roleMapper, UserRoleMapper userRoleMapper) {
        this.roleMapper = roleMapper;
        this.userRoleMapper = userRoleMapper;
    }



    public List<Role> getAllRoles() {

        return roleMapper.selectList(new QueryWrapper<Role>().last("limit 50"));
    }


    public int addRole(Role role) {
        String formatTime = DateTimeUtil.getDateTimeString(LocalDateTime.now());
        role.setAddTime(formatTime);
        role.setUpdateTime(formatTime);
        return roleMapper.insert(role);
    }


    public Role getRole(int roleId) {
        return roleMapper.selectById(roleId);
    }


    public int deleteRole(int roleId) {
        return roleMapper.deleteById(roleId);
    }


    public int updateRole(Role role) {
        Assert.notNull(role.getId(), "非法数据");
        role.setAddTime(null);
        role.setUpdateTime(DateTimeUtil.getDateTimeString(LocalDateTime.now()));
        return roleMapper.updateById(role);
    }
}
